package com.sunda.spmsoversea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmsoversea.entity.OverseaPackageListBox;
import com.sunda.spmsoversea.mapper.OverseaPackageListBoxMapper;
import com.sunda.spmsoversea.service.IOverseaPackageListBoxService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import static com.sunda.spmsoversea.constant.OverseaWhsInConstant.*;
import static com.sunda.spmsoversea.constant.SapConstant.DATA_STATUS_1;

/**
 * <p>
 * 海外仓收货装箱单箱码关系表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-10-22
 */
@Service
public class OverseaPackageListBoxServiceImpl extends ServiceImpl<OverseaPackageListBoxMapper, OverseaPackageListBox> implements IOverseaPackageListBoxService {

    @Override
    public int updateOverseaPackageListBox(String boxNote, String arrivalDate, String userId,
                                           String werksReceive, String whsLocationCodeReceive) {
        if (StringUtils.isEmpty(boxNote) || StringUtils.isEmpty(arrivalDate)){
            return -1;
        }
        QueryWrapper<OverseaPackageListBox> queryWrapper =
                new QueryWrapper<OverseaPackageListBox>().eq("BOX_NOTE", boxNote);
        OverseaPackageListBox overseaPackageListBox = new OverseaPackageListBox();
        overseaPackageListBox.setBoxStatus(RECEIVED);
        overseaPackageListBox.setDataStatus(DATA_STATUS_1);
        overseaPackageListBox.setArrivalDate(arrivalDate);
        overseaPackageListBox.setUpdateUserid(userId);
        overseaPackageListBox.setWerksReceive(werksReceive);
        overseaPackageListBox.setWhsLocationCodeReceive(whsLocationCodeReceive);
        return this.baseMapper.update(overseaPackageListBox, queryWrapper);
    }

    @Override
    public OverseaPackageListBox getOverseaPackageListBox(String boxNote, String boxNoteItem) {
        if (StringUtils.isEmpty(boxNote) || StringUtils.isEmpty(boxNoteItem)){
            return null;
        }
        QueryWrapper<OverseaPackageListBox> queryWrapper =
                new QueryWrapper<OverseaPackageListBox>().eq("BOX_NOTE", boxNote).eq("BOX_NOTE_ITEM", boxNoteItem);
        return this.baseMapper.selectOne(queryWrapper);
    }

    /** 根据箱码 和 箱码明细行号，更新箱码行海外实际收货信息，操作类型说明：ADD 表示箱码明细行上加数量；SUB 表示箱码明细行上减数量； */
    @Override
    public void updateOverseaPackageListBoxQty(String boxNote, String boxNoteItem, double qty, String operateType) {
        if (StringUtils.isEmpty(boxNote) || StringUtils.isEmpty(boxNoteItem)){
            return;
        }
        if (qty <= 0){
            return;
        }
        QueryWrapper<OverseaPackageListBox> queryWrapper =
                new QueryWrapper<OverseaPackageListBox>().eq("BOX_NOTE", boxNote).eq("BOX_NOTE_ITEM", boxNoteItem);
        OverseaPackageListBox overseaPackageListBox = this.baseMapper.selectOne(queryWrapper);
        if (overseaPackageListBox == null){
            return;
        }
        double currentQty = overseaPackageListBox.getActualQtyBasicUnit() == null ? 0.0 : overseaPackageListBox.getActualQtyBasicUnit();
        double targetQty = 0;
        if (ADD.equals(operateType)){
            targetQty = currentQty + qty;
        }
        if (SUB.equals(operateType)){
            if (currentQty <= qty){
                targetQty = 0;
            } else {
                targetQty = currentQty - qty;
            }
        }
        overseaPackageListBox.setActualQtyBasicUnit(targetQty);
        this.baseMapper.update(overseaPackageListBox, queryWrapper);
    }
}
